// components/education-add/index.js
import {timesToDate} from "../../utils/Util";
import {getDictList} from "../../service/Common";
import Notify from "@vant/weapp/notify/notify";
Component({

    /*
    * 默认接收全局样式
    * */
    options: {
        addGlobalClass: true
    },

    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            value: false
        },
        info: {
            type: Object,
            value: {
                schoolNo: 0,
                schoolName: "",
                professionalNo: 0,
                professionalName: "",
                code: "",
                schooling: "",
                grade: ""
            }
        },
        buttonText: {
            type: String,
            value: "完成"
        },
        buttonLoading: {
            type: Boolean,
            value: false
        },
        buttonColor: {
            type: String,
            value: "var(--color-enterprise-primary)"
        }
    },

    /*
    * 熟悉监听
    * */
    observers: {
        "show": function (val) {
            if (val) { //重置
                let info = this.data.info;
                console.log(info)
                if (info && Object.keys(info).length > 0 && info.grade) {
                    let grade = info.grade;
                    this.setData({
                        "_startTime.value": grade[0],
                        "_endTime.value": grade[1]
                    })
                } else {
                    this.setData({
                        info: {
                            schoolNo: 0,
                            schoolName: "",
                            professionalNo: 0,
                            professionalName: "",
                            code: "",
                            schooling: "",
                            grade: ""
                        },
                        "_startTime.value": "",
                        "_endTime.value": ""
                    })
                }
            }
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        /*--组件数据--*/
        _schoolingPicker: { //学历选择器
            show: false,
            loading: false,
            list: []
        },
        _datePicker: { //选择器
            show: false,
            title: "",  //选择器标题
            currentDate: new Date().getTime(),
            minDate: new Date(1970, 1, 1).getTime(),
            maxDate: new Date().getTime(),
        },
        _startTime: {
            title: "开始时间",
            value: ""
        },
        _endTime: {
            title: "结束时间",
            value: ""
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {

        /*
        * 跳转查询页面
        * */
        bindToSelected: function (e) {
            const that = this;
            let name = e.currentTarget.dataset.name;
            if (name === "schoolName") {
                wx.navigateTo({
                    url: "/pages/common/school-search/index",
                    events: {
                        acceptDataFromOpenedPage: function(data) {
                            that.setData({
                                "info.schoolNo": data.sNo,
                                "info.schoolName": data.sName
                            })
                        }
                    },
                })

            } else {
                wx.navigateTo({
                    url: "/pages/common/major-search/index",
                    events: {
                        acceptDataFromOpenedPage: function(data) {
                            that.setData({
                                "info.professionalNo": data.no,
                                "info.professionalName": data.name
                            })
                        }
                    },
                })
            }
        },


        /*
        * 学历选择器
        * */
        bindShowSchoolingPicker: async function () {
            try {
                let list = this.data._schoolingPicker.list;
                this.setData({ "_schoolingPicker.show": true})
                if (list.length === 0) {
                    this.setData({"_schoolingPicker.loading": true});
                    let res = await getDictList("XL");
                    this.setData({
                        "_schoolingPicker.list": res,
                        "_schoolingPicker.loading": false
                    })
                }
            } catch (e) {
                console.error(e)
            }
        },
        onSchoolingPickerConfirm: function (e) {  // 选择器选择
            let detail = e.detail;
            this.setData({
                "_schoolingPicker.show": false,
                "info.schooling": detail.value.text
            });
            this.data.info.code = detail.value.code;
        },
        onSchoolingPickerCancel: function (e) {  // 选择器关闭
            this.setData({"_schoolingPicker.show": false})
        },


        /*
        * 日期选择器展开
        * */
        bindShowDatePicker: function (e) {
            let name = e.currentTarget.dataset.name;
            let title = this.data[name].title;
            this.data._curretName = name;
            this.setData({
                "_datePicker.show": true,
                "_datePicker.title": title
            })
        },
        onDatePickerConfirm: function (e) {   // 选择器选择
            let value = e.detail;
            let name = this.data._curretName;
            this.setData({
                "_datePicker.show": false,
                [name + ".value"]: timesToDate(value).substr(0, 7)
            })
        },
        onDatePickerCancel: function (e) {  // 选择器关闭
            this.setData({"_datePicker.show": false})
        },
        
        /*
        * 验证提交信息
        * */
        onSubmitButton: function () {
            let schoolName = this.data.info.schoolName;
            let professionalName = this.data.info.professionalName;
            let schooling = this.data.info.schooling;
            let startTime = this.data._startTime.value;
            let endTime = this.data._endTime.value;
            let isLoading = this.data.buttonLoading;
            if (isLoading) { //防重复
                return false
            } else if (!schoolName) {
                Notify({
                    message: "请选择学校名称",
                    selector: "#van-education-notify",
                    context: this,
                });
                return false
            } else if (!professionalName) {
                Notify({
                    message: "请选择专业名称",
                    selector: "#van-education-notify",
                    context: this,
                });
                return false
            } else if (!schooling) {
                Notify({
                    message: "请选择学历",
                    selector: "#van-education-notify",
                    context: this,
                });
                return false
            } else if (!startTime) {
                Notify({
                    message: "请选择就读开始时间",
                    selector: "#van-education-notify",
                    context: this,
                });
                return false
            }  else if (!endTime) {
                Notify({
                    message: "请选择就读结束时间",
                    selector: "#van-education-notify",
                    context: this,
                });
                return false
            } else {
                let info = this.data.info;
                info.grade = startTime + ";" + endTime;
                this.triggerEvent("save", info, {}); //组件接收函数写入数据
            }
        }
    }
})

